package com.example.mylibrary.net;

import android.text.TextUtils;

import com.blankj.utilcode.util.LogUtils;

import java.io.IOException;

import okhttp3.Interceptor;
import okhttp3.Request;
import okhttp3.Response;
import okhttp3.ResponseBody;

public class HttpLoggingInterceptor implements Interceptor {
    @Override
    public Response intercept(Chain chain) throws IOException {
        Request request = chain.request();
        Response response = chain.proceed(chain.request());
        String content = response.body().string();
        if (request.url().url().getPath().contains(UrlConstant.GETNAVIGATION)) {
            LogUtils.e("导航请求内容：" + request.toString());
            if (request.headers() != null) {
                StringBuffer sb = new StringBuffer();
                for (String name : request.headers().names()) {
                    sb.append(name + " : " + request.headers().get(name)).append("\n");
                }
                LogUtils.e("导航请求 Head：" + sb.toString());
            }
            if (!TextUtils.isEmpty(content)) {
                LogUtils.e("导航 内容：" + content);
            }
        }

        return response.newBuilder().body(ResponseBody.create(response.body().contentType(), content)).build();
    }
}
